package kkk;

import java.util.ArrayList;
import java.util.Scanner;

class Student {
    private String sid;
    private String name;
    private String age;
    private String day;

    public Student(){

    }

    public Student(String sid, String name, String age, String day) {
        super();
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.day = day;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getday() {
        return day;
    }

    public void setday(String day) {
        this.day = day;
    }

    public static void testStudent(ArrayList<Student> array){
        System.out.println("请输入你的选择：1、添加学生 2、删除学生 3、修改学生 4、查看所有学生 5、退出");
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        switch (line) {
            case "1":
                System.out.println("添加学生");
                addStudent(array);
                break;
            case "2":
                System.out.println("删除学生");
                deletestudent(array);
                break;
            case "3":
                System.out.println("修改学生");
                updatestudent(array);
                break;
            case"4":
                System.out.println("查看所有学生");
                findallstudent(array);
                break;
            case"5":
                System.out.println("谢谢使用");
                break;
        }
    }

    public static void addStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while(true) {
            System.out.println("请输入学生学号：");
            sid = sc.nextLine();
            boolean flag=isUsed(array,sid);
            if(flag) {
                System.out.println("你输入的学号已经被使用，请重新输入");
            }else {
                break;
            }
        }
        System.out.println("请输入学生姓名：");
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String age= sc.nextLine();
        System.out.println("请输入学生出生日期：");
        String day = sc.nextLine();

        Student s = new Student(sid, name, age, day);
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setday(day);
        array.add(s);
        System.out.println("添加学生成功");
        testStudent(array);
    }

    public static boolean isUsed(ArrayList<Student> array, String sid) {
        boolean flag=false;
        for(int i=0;i<array.size();i++) {
            Student s=array.get(i);
            if(s.getSid().equals(sid)) {
                flag=true;
                break;
            }
        }
        return flag;
    }

    public static void findallstudent(ArrayList<Student>array) {
        if(array.size()==0) {
            System.out.println("无信息，请先添加信息再查询");
            testStudent(array);
            return;
        }
        System.out.println("学号\t姓名\t年龄\t出生日期");
        for(int i=0;i<array.size();i++) {
            Student s=array.get(i);
            System.out.println(s.getSid()+" \t"+s.getName()+" \t "+s.getAge()+"岁\t"+s.getday());
            testStudent(array);
        }
    }

    public static void deletestudent(ArrayList<Student>array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号：");
        String sid=sc.nextLine();
        int index=-1;
        for(int i=0;i<array.size();i++) {
            Student s= array.get(i);
            if(s.getSid().equals(sid)) {
                index=i;
                break;
            }
        }
        if(index==-1) {
            System.out.println("该信息不存在，请重新输入");
            deletestudent(array);
            testStudent(array);
        }else {
            array.remove(index);
            System.out.println("删除学生成功");
            testStudent(array);
        }
    }
    public static void updatestudent(ArrayList<Student>array) {
        Scanner sc=new Scanner(System.in);
        String sid;
        while(true) {
            System.out.println("请输入你要修改的学生的学号：");
            sid = sc.nextLine();
            boolean flag=isUsed(array,sid);
            if(!flag) {
                System.out.println("你输入的学号不存在，请重新输入");
            }else {
                break;
            }
        }
            System.out.println("请输入学生的新姓名：");
            String name = sc.nextLine();
            System.out.println("请输入学生的新年龄：");
            String age = sc.nextLine();
            System.out.println("请输入学生的新出生日期：");
            String day = sc.nextLine();
            Student s = new Student(sid, name, age, day);
            s.setSid(sid);
            s.setName(name);
            s.setAge(age);
            s.setday(day);

        for(int i=0;i<array.size();i++) {
            Student student=array.get(i);
            if(student.getSid().equals(sid)) {
                array.set(i, s);
                testStudent(array);
                break;
            }
        }
        System.out.println("修改学生成功");
        testStudent(array);

    }

}

